/// @tags: Trie
#include <cstdio>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::string;
using std::vector;

namespace BlueQuantum {

constexpr int MAXN = 1e5 + 3;

int n, q;
int opt[MAXN], ans[MAXN];
string str[MAXN];
vector<int> del[MAXN], add[MAXN];

struct Query {
  string ip;
  int id;
} Q[MAXN];

struct Node {
  int ch[2], end, cnt, tag;
  inline void applyTag(int val) { cnt += val, tag += val; }
} tr[MAXN * 32];

inline void pushDown(int x) {
  static int cnt = 0;
  if (!tr[x].ch[0]) tr[x].ch[0] = ++cnt;
  if (!tr[x].ch[1]) tr[x].ch[1] = ++cnt;
  if (tr[x].tag == 0) return;
  if (!tr[tr[x].ch[0]].end) tr[tr[x].ch[0]].applyTag(tr[x].tag);
  if (!tr[tr[x].ch[1]].end) tr[tr[x].ch[1]].applyTag(tr[x].tag);
  tr[x].tag = 0;
}

inline void insert(const string &str, int val) {
  int cur = 0;
  for (auto c : str) {
    pushDown(cur);
    cur = tr[cur].ch[c - '0'];
  }
  tr[cur].end += val, tr[cur].tag++, tr[cur].cnt++;
}

inline int query(const string &str) {
  int cur = 0;
  for (auto c : str) {
    pushDown(cur);
    cur = tr[cur].ch[c - '0'];
  }
  return tr[cur].cnt;
}

inline int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    char s[6];
    cin >> s >> str[i];
    opt[i] = (s[0] == 'A') ? 1 : -1;
  }
  for (int i = 1; i <= q; ++i) {
    int l, r;
    cin >> Q[i].ip, Q[i].id = i;
    cin >> l >> r;
    del[l].push_back(i), add[r].push_back(i);
  }
  for (int i = 1; i <= n; ++i) {
    insert(str[i], opt[i]);
    for (auto j : del[i]) ans[Q[j].id] -= query(Q[j].ip);
    for (auto j : add[i]) ans[Q[j].id] += query(Q[j].ip);
  }
  for (int i = 1; i <= q; ++i) cout << ans[i] << '\n';
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P5460 [BJOI2016] IP地址.in", "r", stdin);
  freopen("P5460 [BJOI2016] IP地址.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
